package com.yz.food.cms.service.impl;

import com.yz.food.cms.dao.RecipesMapper;
import com.yz.food.cms.pojo.Recipes;
import com.yz.food.cms.service.RecipesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecipesServiceImpl implements RecipesService {
    @Autowired
    private RecipesMapper recipesMapper;

    /**
     * 获取所有的食谱
     * @return
     */
    @Override
    public List<Recipes> getAll() {
        return recipesMapper.getAll();
    }

    /**
     * 更新和添加食谱
     * @param recipes
     * @return
     */
    @Override
    public int updateRecipes(Recipes recipes) {
        if (recipes.getId() == null ) {
            // 新添加
            Recipes newRecipes = new Recipes();
            newRecipes.setContent(recipes.getContent());
            newRecipes.setStatus(1);
            newRecipes.setTitle(recipes.getTitle());
            newRecipes.setImgUrl(recipes.getImgUrl());
            return recipesMapper.insertSelective(newRecipes);
        }
        Recipes newRecipes = new Recipes();
        newRecipes.setId(recipes.getId());
        newRecipes.setContent(recipes.getContent());
        newRecipes.setStatus(1);
        newRecipes.setTitle(recipes.getTitle());
        newRecipes.setImgUrl(recipes.getImgUrl());
        return recipesMapper.updateByPrimaryKeySelective(newRecipes);
    }

    /**
     * 根据id删除
     * @param recipesId
     * @return
     */
    @Override
    public int deleteById(Integer recipesId) {
        return recipesMapper.deleteByPrimaryKey(recipesId);
    }
}
